import { io } from 'socket.io-client'
import Vue from 'vue/types/index'
import { Message, Myapp, Name, Nametype, Userwin } from '../interface'
const Vuez: typeof Vue = (window as any).Vue
const search = location.search
let port = 5656
const f = search.slice(1).split('&')
f.forEach(v => {
    if (v.includes('port')) {
        const [_z, p] = v.split('=')
        port = Number(p)
    }
})
const url = process.env.NODE_ENV !== 'development' ? `http://localhost/api` : `http://localhost:${port}`
const socket = io(url)
console.log(socket)
socket.on("connect", () => {
    console.log(socket.id); // x8WIv7-mJelg7on_ALbx
});




function setMessage(type: string, message: string) {
    socket.send(JSON.stringify({ type, message }))
}

const d = document.querySelector('button')

function clickbutton() {

}



window.onload = function () {
    const app = new Vuez<{ myapp: Myapp, userwin: Userwin, list: Nametype[] }>({
        el: '#app',
        data: {
            myapp: {
                name: ''
            },
            userwin: {
                type: 'win32',
                version: '',
                arch: 'x64'
            },
            list: []
        },
        mounted() {
            socket.on('A0', (data: Myapp) => {
                this.myapp = data
            })
            socket.on('A1', (data: Userwin) => {
                this.userwin = data
            })
            socket.on('A2', (data: Name[]) => {
                this.list = data
            })

            socket.on('data', this.data)
            socket.send('A0')
            socket.send('A1')
            socket.send('A2')
        },
        methods: {
            sendmessage() {

            },
            data(...data) {
                const [type, message] = data
                console.log(type)
                console.log(message)
            }
        }
    })
}

